<script src="https://cdn.bootcss.com/simplemde/1.11.2/simplemde.min.js"></script>
<script src="https://cdn.bootcss.com/codemirror/5.48.2/codemirror.js"></script>
<script src="/static/js/inline-attachment.js"></script>
<script src="/static/js/codemirror-4.inline-attachment.js"></script>
<script src="https://cdn.bootcss.com/marked/0.6.2/marked.min.js"></script>
<script src="https://cdn.bootcss.com/limonte-sweetalert2/7.33.1/sweetalert2.all.min.js"></script>
<script src="/static/js/form.js"></script>
<script src="/static/js/error.js"></script>
<script type="text/javascript">
    // 图片拖拽上传参考：
    // 1. https://github.com/Rovak/InlineAttachment
    // 2. https://learnku.com/articles/25988
    var simplemde;
    $(document).ready(function () {
        simplemde = new SimpleMDE({
            element: document.getElementById("editor"),
            autosave: {
                enabled: false,
                delay: 1,
                uniqueId: "#editor",
            },
            blockStyles: {
                bold: "__",
                italic: "_"
            },
            forceSync: true,
            hideIcons: ["guide", "heading"],
            showIcons: ['code', 'table'],
            indentWithTabs: false,
            insertTexts: {
                horizontalRule: ["", "\n\n-----\n\n"],
                image: ["![](http://", ")"],
                link: ["[", "](http://)"],
                table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text     | Text      | Text     |\n\n"],
            },
            parsingConfig: {
                allowAtxHeaderWithoutSpace: true,
                strikethrough: false,
                underscoresBreakWords: true,
            },
            placeholder: "请使用Markdown格式书写:), 代码片段黏贴时请注意使用高亮语法，图片支持拖拽和粘贴上传。",
            // 在编辑页面生成预览
            previewRender: function(plainText, preview) { // Returns HTML from a custom parser, Async method
                setTimeout(function(){
                    preview.innerHTML = marked(plainText);
                }, 250);
                return "预览生成中......";
            },
        });

        inlineAttachment.editors.codemirror4.attach(simplemde.codemirror, {
            uploadUrl: '/image/upload',
            extraParams: {
                // csrf token here
            },
            onFileUploadResponse: function(xhr) {
                var result = JSON.parse(xhr.responseText),
                    filename = result[this.settings.jsonFieldName];

                if (result && filename) {
                    var newValue;
                    if (typeof this.settings.urlText === 'function') {
                        newValue = this.settings.urlText.call(this, filename, result);
                    } else {
                        newValue = this.settings.urlText.replace(this.filenameTag, filename);
                    }
                    var text = this.editor.getValue().replace(this.lastValue, newValue);
                    this.editor.setValue(text);
                    this.settings.onFileUploaded.call(this, filename);
                }
                return false;
            }
        });
    });
</script>